//
//  UIColor+Mango.m
//  Mango
//
//  Created by 胡超 on 2018/1/29.
//  Copyright © 2018年 胡超., ltd. All rights reserved.
//

#import "UIColor+Mango.h"

@implementation UIColor (Mango)

+ (UIColor * _Nonnull (^)(int, int, int))RGB {
    return ^id(int r, int g, int b) {
        return [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:1.0];
    };
}

+ (UIColor * _Nonnull (^)(int, int, int, float))RGBA {
    return ^id(int r, int g, int b, float a) {
        return [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a];
    };
}

+ (UIColor * _Nonnull (^)(NSString * _Nonnull))Hex {
    return ^id(NSString *hexStr) {
        NSArray *arr = [hexStr componentsSeparatedByString:@","];
        if (arr.count > 1) {
            return self.HexA(arr.firstObject, [arr.lastObject floatValue]);
        } else {
            return self.HexA(arr.firstObject, 1.0);
        }
    };
}

+ (UIColor * _Nonnull (^)(NSString * _Nonnull, float))HexA {
    return ^id(NSString *hexStr, float a) {
        if (hexStr.length != 7 || ![hexStr hasPrefix:@"#"]) {
            return UIColor.blackColor;
        }

        hexStr = [hexStr substringFromIndex:1];
        long hex = strtol(hexStr.UTF8String, 0, 16);
        int r = (hex & 0xff0000) >> 16;
        int g = (hex & 0x00ff00) >> 8;
        int b = (hex & 0x0000ff);
        return self.RGBA(r,g,b,a);
    };
}

@end
